<?php

/**
	 * @see Bike_Form_Renderer_ExtJs_Element
	 */
	require_once 'Bike/Form/Renderer/ExtJs/Element.php';
	
	
	abstract class Bike_Form_Renderer_ExtJs_Field_Abstract extends Bike_Form_Renderer_ExtJs_Element
	{
		protected function getExtJsSettings()
		{
			$value = ($this->getElement()->showForRepeating()) 
								? (($this->getElement()->showFilteredValue()) 
									? $this->getElement()->getValue() 
									: $this->getElement()->getUnfilteredValue()) 
								: '';
			$required = $this->getElement()->isRequired();
			$allowBlank = ($required) ? !$required : $this->getElement()->getAllowEmpty();
			
			return array_merge((array) parent::getExtJsSettings(), array(
				'fieldLabel' => $this->getElement()->getLabel(),
				//'id' => $this->getElement()->getId(),
				'allowBlank' => (bool) $allowBlank,
				'required' => (bool) $required,
				'activeError' => (sizeof($this->getElement()->getMessages())) ? '<ul><li>'.join('</li><li>', $this->getElement()->getMessages()).'</li></ul>' : '',
				'value' => $this->parseInt($value),
				'readOnly' => (bool) $this->getElement()->readOnly,
				'disabled' => (bool) $this->getElement()->disabled,
				'description' => $this->getElement()->getDescription(),
				'boxLabel' => $this->getElement()->getMark(),
				'boxLabelAlign' => ($this->getElement()->getMarkAlign() == 'after') ? 'after' : 'before',
			));
		}
		
		protected function parseInt($value)
		{
			if(!$this->getElement()->getParseInt())
			{
				return $value;
			}
			
			if(is_string($value))
			{
				$len = strlen($value);
				$intValue = intval($value);
				$strValue = (string) $intValue;
				if($strValue === $value && strlen($strValue) == $len)
				{
					$value = $intValue;
				}
			}
			elseif(is_array($value) || is_object($value))
			{
				foreach($value as $key => $val)
				{
					$value[$key] = $this->parseInt($val);
				}
			}
			
			return $value;
		}
	}
